import time

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options


class PeiYinShenQiSelenium():
    def __init__(self,driver_path):
        '''
        chatgpt 调用网页读写内容
        :param chatgpt_url: chatgpt网站地址
        :param local_port: chrome 本地调试端口
        :param driver_path: chrome webdriver路径
        '''
        self.tts_url = "https://peiyinshenqi.com/#/tts/tools"
        self.peiyin_url = 'https://peiyinshenqi.com/#/tts/index'
        self.browser = self.get_browser(chromedirver_path=driver_path)
        self.browser.get(self.tts_url)
        time.sleep(2)


    def get_tts_wenan(self,video_url):
        '''
        获取短视频文案
        :param video_url:
        :return:
        '''
        self.browser.refresh()
        time.sleep(3)
        # 1.切换到文案提取页面
        menuBttons = self.browser.find_elements(By.XPATH,'//li[@class="el-menu-item"]')
        targetButton = ""
        for button in menuBttons:
            if button.text.strip() == "文案提取":
                targetButton = button
                break

        targetButton.click()
        time.sleep(2)

        #2. 切换到短视频链接
        radioButton = self.browser.find_elements(By.XPATH,'//label[contains(@class,"el-radio")]')[1]
        radioButton.click()

        time.sleep(2)
        #3. 获取输入狂
        textareaElement = self.browser.find_element(By.XPATH,'//textarea[@class="el-textarea__inner"]')
        textareaElement.send_keys(video_url)

        time.sleep(2)
        #4. 点击开始提取按钮
        submitButtons = self.browser.find_elements(By.XPATH,'//button[@class="el-button el-button--primary"]')
        targetSubmitButton = ""
        for button in submitButtons:
            if button.text == "开始提取文案":
                targetSubmitButton = button
        targetSubmitButton.click()

        time.sleep(2)
        #5. 等待2分钟，如果还没有结果，就返回空
        check_count = 0
        resultTextareaElements = self.browser.find_elements(By.XPATH,'//textarea[@class="el-textarea__inner"]')
        while len(resultTextareaElements) <2 and check_count <=120:
            check_count +=1
            time.sleep(1)
            resultTextareaElements = self.browser.find_elements(By.XPATH, '//textarea[@class="el-textarea__inner"]')
        wenan = ""
        if len(resultTextareaElements) > 1:
            wenan = resultTextareaElements[1].get_attribute("value")

        return wenan






    def get_browser(self, chromedirver_path, local_port=5001):
        '''
        初始话webdriver browser
        :param chromedirver_path:
        :param local_port:
        :return:
        '''
        if 1:
            options = webdriver.ChromeOptions()
            options.add_argument(r'--user-data-dir=E:\chrome_data\xulaoshi')
            options.add_experimental_option("debuggerAddress", "127.0.0.1:5001" + str(local_port))
            options.add_argument(
                "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/2blabla")
            browser = webdriver.Chrome(chrome_options=options, executable_path=chromedirver_path)

        else:
            options = Options()
            browser = webdriver.Firefox(options=options,firefox_profile=r"F:\FireFoxNew")
            browser.implicitly_wait(3)

        return browser


    def get_role(self,role_name):
        '''
        获取
        :return:
        '''

        role_divs = self.browser.find_elements(By.XPATH,"//div[@class='sub-item-container']")
        for role_d in role_divs:
            if role_name in role_d.text:
                role_d.click()
                time.sleep(1)


    def make_new_mp3(self,text,role_name):
        '''
        :param text: 待配音文本
        :param people:角色
        :return:
        '''
        self.browser.get(self.peiyin_url)
        time.sleep(2)
        self.get_role(role_name)
